package com.huaruan.youdi.http.okhttp;

import android.content.Context;

import java.util.SortedMap;
import java.util.TreeMap;

import okhttp3.RequestBody;

/**
 * Created by yushilong on 2015/5/14.
 */
public class HttpPageUtils {
    private int page = 1;
    private int pagesize = 30;
    private String url;
    private boolean isPostMethod;
    private RequestBody requestBody;//为null则是get请求 否则post请求
    private Class<?> modelClass;//json字符串对应的实体类
    private HttpCallback callback;
    private SortedMap<String, Object> sortedMap;
    @Deprecated
    private String pageKey = "pageNo";//default page
    private String pageSizeKey = "pageSize";//default pagesize
    private Context context;
    boolean isNeedPageKey = true, isNeedPageSizeKey = true;

    @Deprecated
    public HttpPageUtils(Context context, String url, SortedMap sortedMap, RequestBody requestBody, Class<?> tClass, HttpCallback httpCallback) {
        this.context = context;
        this.url = url;
        this.sortedMap = sortedMap;
        this.requestBody = requestBody;
        this.modelClass = tClass;
        this.callback = httpCallback;
    }

    public HttpPageUtils(Context context) {
        this.context = context;
    }

    /**
     * 针对于EasyRecyclerView这样的控件可以废弃isReachTheBottom和isRequesting<br/>
     */
    public void async2() {
        if (sortedMap == null) {
            sortedMap = new TreeMap<>();
        }
        if (isNeedPageKey)
            sortedMap.put(pageKey, page + "");
        if (isNeedPageSizeKey)
            sortedMap.put(pageSizeKey, pagesize + "");
        HttpUtils.Builder builder = new HttpUtils.Builder(context)
                .postMethod()
                .url(url)
                .params(sortedMap)
                .modelClass(modelClass)
                .callback(callback);
        if (isPostMethod) {
            builder = builder.postMethod();
        }
        builder.start();
    }

    public void first() {
        page = 1;
        async2();
    }

    public void next() {
        page = page + 1;
        async2();
    }

    public HttpPageUtils url(String url) {
        this.url = url;
        return this;
    }

    public HttpPageUtils params(SortedMap map) {
        this.sortedMap = map;
        return this;
    }

    public HttpPageUtils postMethod() {
        isPostMethod = true;
        return this;
    }

    public SortedMap<String, Object> getParams() {
        return sortedMap;
    }

    public HttpPageUtils modelClass(Class<?> modelClass) {
        this.modelClass = modelClass;
        return this;
    }

    public HttpPageUtils pageKey(String pageKey) {
        this.pageKey = pageKey;
        return this;
    }

    public HttpPageUtils pageSizeKey(String pageSizeKey) {
        this.pageSizeKey = pageSizeKey;
        return this;
    }

    public HttpPageUtils pageSize(int pageSize) {
        this.pagesize = pageSize;
        return this;
    }

    public HttpPageUtils page(int page) {
        this.page = page;
        return this;
    }

    public HttpPageUtils context(Context context) {
        this.context = context;
        return this;
    }

    public HttpPageUtils needPageKey(boolean needPageKey) {
        this.isNeedPageKey = needPageKey;
        return this;
    }

    public HttpPageUtils needPageSizeKey(boolean needPageSizeKey) {
        this.isNeedPageSizeKey = needPageSizeKey;
        return this;
    }

    public int getPage() {
        return this.page;
    }

    public int getPageSize() {
        return this.pagesize;
    }

    public HttpPageUtils callback(HttpCallback httpCallback) {
        this.callback = httpCallback;
        return this;
    }

    public boolean isFirstPage() {
        return page == 1;
    }
}
